<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>draw.io Editor</title>
<script type="text/javascript">
	// To Avoid NPE in connectUtils
	urlParams = {};
</script>
<script src="connectUtils-1-4-8.js" type="text/javascript"></script>
<style type="text/css">
	html, body {
	  height:100%;
	  overflow:hidden;
	}
	body {
		background-color:#ffffff;
		background-image:url(/images/drawlogo-text-bottom.svg);
		background-repeat:no-repeat;
		background-position:center;
		background-size:128px;
		font-family:Arial,Sans-Serif;
		overflow:hidden;
		width:100%;
		margin:0;
	}
</style>
</head>
<body>
<script type="text/javascript">
	//Logs uncaught errors
	window.onerror = function(message, url, linenumber, colno, err)
	{
		message = 'Confluence Cloud Editor: ' + ((message != null) ? message : '');
		
		AC.logError(message, url, linenumber, colno, err);
	};
	
	var xdm_e = AC.getSiteUrl();
	var baseUrl = AC.getBaseUrl();
	var license = AC.getUrlParam('lic', false);
	var connectUrl = baseUrl + '/atlassian-connect';
	var head = document.getElementsByTagName('head')[0];
	var licenseValid = true;
	
	var script = document.createElement('script');
	script.setAttribute('data-options', 'resize:false;margin:false');
	
	if (!licenseValid && license != null && xdm_e != null)
	{
		if (license == 'none')
		{
			var hostParse = document.createElement('a');
			hostParse.href = xdm_e;
			var hostname = hostParse.hostname;
			
			if (hostname != null)
			{
 				if (hostname.indexOf('.ngrok.io') > -1)
				{
					console.log("License status = ", license);
				}
				else
				{
					var xhr = new XMLHttpRequest();
		
					xhr.onreadystatechange = function()
					{
					    if (xhr.readyState == XMLHttpRequest.DONE && xhr.status >= 200 && xhr.status <= 299)
					    {
					        var resp = xhr.responseText;
		
							if (resp != null && resp.length > 0)
							{
								var lic = JSON.parse(resp);
								
								if (lic != null && lic.atlasCloudLic != null)
								{
									licenseValid = true;
								}
							}
					    }
					};
		
					xhr.open('POST', '/license?domain=' + hostname, true);
					xhr.send(null);
				}
			}
		}
		else
		{
			licenseValid = true;
		}
	}

	// Main
	script.onload = function()
	{
		if (!licenseValid)
		{
			setTimeout(function()
			{
				if (!licenseValid)
				{
					alert("Please install a license for the draw.io app");
					AP.dialog.close();
				}
			}, 3000);
		}

		AP.resize('100%', '100%');

		var config = null;
		var lang = null;
		var allDone = 0;
		
		var startEditor = function () 
		{
			allDone++;
			
			if (allDone == 2)
			{
				var isCustom = AC.getUrlParam('custom');
				
				if (isCustom == "1") 
				{
					var contentId = AC.getUrlParam('contentId') || AC.getUrlParam('custContentId');
					AP.dialog.getCustomData(function (customData) 
					{
						AC.initAsync(baseUrl, customData.contentId || customData.custContentId || contentId, customData.macroData, config, lang);
					});
				}
				else
				{
					AC.initAsync(baseUrl, null, null, config, lang);
				}
			}
		}
		
		AP.user.getLocale(function(locale)
		{
			lang = locale;
			startEditor();
		});	
		
		AP.request({
            type: 'GET',
            url: '/rest/api/content/search?cql=type%3Dpage%20and%20space%3DDRAWIOCONFIG%20and%20title%3DConfiguration', //type=page and space=DRAWIOCONFIG and title=Configuration. Search doesn't return 404 if not found
            contentType: 'application/json;charset=UTF-8',
            success: function (resp) 
            {
                resp = JSON.parse(resp);
                
                if (resp.size == 1)
               	{
                	var configPageId = resp.results[0].id;
                	//load the configuration file
            		AP.request({
                        type: 'GET',
            			url: '/download/attachments/' + configPageId + '/configuration.json',
                        contentType: 'application/json;charset=UTF-8',
                        success: function (fileContent) 
                        {
                        	config = fileContent; 
                           	startEditor();
            			},
            			error: startEditor //if there is an error loading the configuration, just load the editor normally. E.g., 404 when the space doesn't exist
            		});
                	
               	}
                else 
               	{
                	startEditor();
               	}
			},
			error: startEditor //if there is an error loading the configuration, just load the editor normally. E.g., 404 when the space doesn't exist
		});
	};
	script.src = 'https://connect-cdn.atl-paas.net/all.js';
	head.appendChild(script);
	
	var link = document.createElement('link');
	link.type = 'text/css';
	link.rel = 'stylesheet';
	link.href = connectUrl + '/all.css';
	head.appendChild(link);
</script>
</body>
</html>
